package org.tacticalTroopers.jme.server.team;

import java.util.HashMap;
import java.util.Map;

import org.tacticalTroopers.jme.common.message.Teams;
import org.tacticalTroopers.jme.server.object.ServerPlayer;

import com.jme3.network.message.Message;


public class TeamManager {


	public static final String TEAM1 = "Red";
	public static final String TEAM2 = "Blue";

	
	Map<String,Team> idToTeams = new HashMap<String,Team>();
	//Map<ServerPlayer,Team> playerToTeam = new HashMap<ServerPlayer,Team>();
	Teams teams= new Teams();
	Integer nextTeamId ;
	Boolean teamsNeedToBroadcasted = false;

	public TeamManager(){
		idToTeams.put(TEAM1,new Team().setId(TEAM1));
		idToTeams.put(TEAM2,new Team().setId(TEAM2));
	}


	public void add(ServerPlayer p) {
		switchTeam(p,TEAM1);		
		teamsNeedToBroadcasted=true;
		//playerToTeam.put(p, one);
	}


	public Boolean belongToSameTeam(ServerPlayer player1, ServerPlayer player2) {
		return getTeam(player1)==getTeam(player2);
	}


	public Team getTeam(ServerPlayer player) {
		return idToTeams.get(teams.getPlayerTeam(player.getId().getValue()));//playerToTeam.get(player);
	}
	
	public void switchTeam(ServerPlayer player,String teamId){
		teams.setPlayerTeam(player.getId().getValue(), teamId);
		//playerToTeam.put(player, idToTeams.get(teamId));
		teamsNeedToBroadcasted=true;
	}
	
	
	public Boolean isTeamsNeedToBroadcasted() {
		return teamsNeedToBroadcasted;
	}


	public void setTeamsNeedToBroadcasted(Boolean teamsNeedToBeSend) {
		this.teamsNeedToBroadcasted = teamsNeedToBeSend;
	}


	public void switchTeam(ServerPlayer player){
		Team t = getTeam(player);
		if(t.getId().equals(TEAM1)){
			teams.setPlayerTeam(player.getId().getValue(), TEAM2);
		}else{
			teams.setPlayerTeam(player.getId().getValue(), TEAM1);
		}
		teamsNeedToBroadcasted=true;
	}


	public void remove(ServerPlayer player) {
		teams.removePlayerTeam(player.getId().getValue());
	}


	public Message getTeamsMessage() {
		return teams;
	}




}
